﻿/* 
 *mytab v1.0
 *Copyright (c) 2011 sdata007 
 *Creation date 2012-2-11
 *该组件基于jQuery，属于DataTable组件
*/
var type =function(o){   
		//"Boolean","Number","String","Function","Array","Date","RegExp","Object"
		var t=Object.prototype.toString.call(o),l=t.length;
		return o==null?String(o):t.slice(8,l-1);
};
var	DataTable = function(opt){
		//定义配置
		var tbBox = opt.tbBox,//表格容器
			tbId = opt.tbId,//表格Id,用来绑定具体的表格
			tbColumn = opt.tbColumn,//表头标题
			tbData = opt.tbData;//表格数据或url
		var tbColLen = tbColumn.length,
			dLength = 0;
		var $tbBox = $("#" + tbBox);
		//加载数据
		var loadTbData = function(data){
			var tb_data = data,
				tb_data_length = tb_data.length;
			var arrTableHtml = [];
		//创建表格
		//表头
		arrTableHtml.push('<table border="0" cellspacing="0" cellpadding="0" class="netable">');
		arrTableHtml.push('<thead>');
		arrTableHtml.push('<tr>');
		$.each(tbColumn,function(i,val){
			arrTableHtml.push('<th key="'+(val.key || "")+'" class="netable_th" width="'+(val.width || '')+'">'+val.label+'</th>')
		});
		arrTableHtml.push('</tr>');
		arrTableHtml.push('</thead>');
		//表体
		arrTableHtml.push('<tbody>');
		//行
		$.each(data,function(i,o){
			//隔行换色
			var trClass="netable_odd"; 
			if(i%2) trClass="netable_event";
			arrTableHtml.push('<tr class="netable_body_tr ' + trClass + '">');
			for(var j=0; j<tbColLen; j++){
				//字段获取
				var name = tbColumn[j].key || "",
					value = o[name],
					formatter = tbColumn[j].formatter;
				if(formatter){value = formatter(o,i) || "";}
				//数据填充
				arrTableHtml.push('<td class="netable_td netable_td_main" width="'+(tbColumn[j].width || '')+'"><div class="netable_field">'+value+'</div></td>');
			}
			arrTableHtml.push('</tr>');
		});
		arrTableHtml.push('</tbody>');
		arrTableHtml.push('</table>');
		$tbBox.html(arrTableHtml.join(""));
		var tr = $tbBox.find(".netable_body_tr"),
			td = $tbBox.find(".netable_td");
		tr.hover(function(){
			$(this).addClass("netable_hover");				  
		},function(){
			$(this).removeClass("netable_hover");	
		});
	};
	tbData && loadTbData(tbData);
	var _return={reloadData:loadTbData};
	return _return;
};
var myPage = function(opt){
	var selfFn=arguments.callee;
	var pageBox = opt.pageBox,//分页容器
		totalRecord = opt.totalRecord,//总记录
		pageSize = opt.pageSize || 15,//分页大小
		pageIndex = opt.pageIndex || 1,//当前页面
		pageNum = opt.pageNum || 10,//显示标签数量
		totalPage=opt.totalPage || Math.ceil(totalRecord/pageSize),
		pageSpan = opt.pageSpan || "首页,上一页,{i},下一页,尾页",
		changePage=opt.changePage,
		callback=opt.callback;
		var pageSpanArry = pageSpan.split(",");
		if(pageIndex<0)pageIndex=1;
		if(pageIndex>totalPage)pageIndex=totalPage;
	var halfi=parseInt((pageNum-1)/2);
	var lefti=pageIndex-halfi;//左下标
	if(lefti<1)lefti=1;
	var righti=lefti+pageNum-1;//右下标
	if(righti>totalPage)righti=totalPage;
	var $pageBox=pageBox;
	if(type(pageBox)=="String"){
		$pageBox=$("#"+pageBox);
	}
	//html组建
	var pageHtml=[];
	//查询结果
	pageHtml.push('<div class="dataPage">');
	pageHtml.push('<span class="dataPage_infor">查询结果：<span class="dadaPage_numfor">( '+pageIndex+' / '+totalPage+'页 共'+totalRecord+'条 每页'+pageSize+'条 )</span></span>');
	//首页，上一页
	if(pageIndex==1){
		pageHtml.push(' <span class="dataPage_btn_first dataPage_btn_off">'+pageSpanArry[0]+'</span> <span class="dataPage_btn_pre dataPage_btn_off">'+pageSpanArry[1]+'</span> ');
	}else{
		pageHtml.push(' <a href="#" class="dataPage_btn_first" page="1">'+pageSpanArry[0]+'</a> <a href="#" class="dataPage_btn_pre" page="'+(pageIndex-1)+'">'+pageSpanArry[1]+'</a> ');
	}
	//数字分页
	for(var i=lefti;i<=righti;i++){
		if(i==pageIndex){
			pageHtml.push('<span class="dataPage_num dataPage_num_on">'+pageSpanArry[2].replace("{i}",i)+'</span> ');
		}else{
			pageHtml.push('<a href="#" class="dataPage_num" page="'+i+'">'+pageSpanArry[2].replace("{i}",i)+'</a> ');
		}
	}
	//下一页，尾页
	if(pageIndex==totalPage){
		pageHtml.push(' <span class="dataPage_btn_next dataPage_btn_off">'+pageSpanArry[3]+'</span> <span class="dataPage_btn_last dataPage_btn_off">'+pageSpanArry[4]+'</span> ');
	}else{
		pageHtml.push(' <a href="#" class="dataPage_btn_next" page="'+(pageIndex+1)+'">'+pageSpanArry[3]+'</a> <a href="#" class="dataPage_btn_last" page="'+totalPage+'">'+pageSpanArry[4]+'</a> ');
	}
	//go
	pageHtml.push(' <input class="dataPage_goindex" type="text" value="跳转到" /> ');
	pageHtml.push('</div>');
	$pageBox.html(pageHtml.join(""));
	//a事件
	$pageBox.find("a").click(function(e){
		var num=parseInt($(this).attr("page")) || 0;
		if(num<1)num=1;
		if(num>totalPage)num=totalPage;
		opt.pageIndex=num;
		changePage(num);
		selfFn(opt);
		return false;
	});
	//go事件
	var goindex=$pageBox.find(".dataPage_goindex");
	goindex.click(function(e){
		var val=$(this).val();
		if(val=="跳转到" || val==""){
			$(this).val("");
		}
		if(!$(this).hasClass("dataPage_goindex_focus")){
			$(this).addClass("dataPage_goindex_focus");
		}
	});
	goindex.keydown(function(e){
		if(e.keyCode==13){//enter
			return false;
		}
	});
	goindex.keyup(function(e){
		if(e.keyCode==13){//enter
			var num=parseInt($(this).val()) || 0;
			if(num<1)num=1;
			if(num>totalPage)num=totalPage;
			opt.pageIndex=num;
			changePage(num);
			selfFn(opt);
			return false;
		}
	});
	callback && callback(pageIndex);
		
};
var myDataTableExtend = function(opt){
	var myDataTable = DataTable($.extend(opt,{data:null}));
	var $tableCon = $("#"+opt.tbBox);
	nd_global.dataBoxNull($tableCon);//空数据容器设置
	var tbData = opt.tbData;
	if(type(tbData)=="String"){//ajaxURL数据请求
		//数据获取
		var url=tbData;//获取url
		if(opt.isPage){
			url=nd_core.setUrlParams("pageIndex","1",url);
			url=nd_core.setUrlParams("pageRows",(opt.pageSize || "15"),url);
		}
		$.getJSON(url,function(result){
			if(result.operateFlg=="SS"){
				var arrData = result.content;
					myDataTable.reloadData(arrData);
				if(opt.isPage && result.pageNum>1){//分页
					if($("#page_"+opt.tbBox).size()<1){
						$tableCon.after('<div id="page_'+opt.tbBox+'"></div>');
					}
					$("#page_"+opt.tbBox).show();
					//分页调用
					myPage({
						pageBox:'page_'+opt.tbBox,
						totalRecord:parseInt(arrData.totalRows),//总记录数
						pageSize:parseInt(arrData.pageRows),//分页大小
						pageIndex:parseInt(arrData.pageIndex),//当前页
						totalPage:parseInt(arrData.pageNum),//总分页数
						changePage:function(i){
							var urlpage = url;
								urlpage = nd_core.setUrlParams("pageIndex",i,urlpage);
							$.getJSON(urlpage,function(result){
								var arrData = result.content;
								myDataTable.reloadData(arrData);
							});
						}
					});
				}else{
					$("#page_"+opt.tbBox).empty();
					$("#page_"+opt.tbBox).hide();
				}
			}
		});
	}else{
		var page_size = opt.pageSize || 15
		var dlen = tbData.length;
		myDataTable.reloadData(tbData.slice(0,page_size));//加载数据
		if(opt.isPage && dlen>page_size){//分页
			if($("#page_"+opt.tbBox).size()<1){
				$tableCon.after('<div id="page_'+opt.tbBox+'"></div>');
			}
			$("#page_"+opt.tbBox).show();
			//分页调用
			myPage({
				pageBox:'page_'+opt.tbBox,
				totalRecord:dlen,//总记录数
				pageSize:page_size,//分页大小
				pageIndex:1,//当前页
				changePage:function(i){
					myDataTable.reloadData(tbData.slice((i-1)*page_size,i*page_size));
				}
			});
		}else{
			$("#page_"+opt.tbBox).empty();
			$("#page_"+opt.tbBox).hide();
		}
	}
};
var fnTestTable = function(id) {
		//定义列
		var myColumn=[
			{key:"dateOfEntrant",  width:100, label:"社会关系"},
			{key:"empEmail", width:50, label:"姓名"},
			{key:"employeesCode", width:120, label:"年龄"},
			{key:"orgName",width:80, label:"是否提醒"}
		];
		//生成表格
		var myDataTable = myDataTableExtend({
			tbBox: id,
			tbColumn: myColumn,
			isPage:true,
			pageSize:15,
			tbData: "http://172.21.17.138/ehr/Service.nd?jsoncallback?&act=inquireEmployeesByOrgId&orgId=2208&viewAll=1"
		});
		// 返回表格
		return myDataTable;
	};